package classes;

import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;

public class Coordenada {
	// private static int metadeAnuncioX = 99;
	// private static int metadeAnuncioY = 49;
	// private static int espacoBranco = 20;
	private static int constanteEspacamentoX = 218;
	private static int constanteEspacamentoY = 118;

	public void movePrimeiroAnuncio(Robot r) {
		r.mouseMove(840, 306);
	}

	public void moveProximoAnuncio(Robot r) {
		r.mouseMove(pegaValorX() + constanteEspacamentoX, 306);
	}

	public void moveProximaLinha(Robot r) {
		r.mouseMove(840, pegaValorY() + constanteEspacamentoY);
	}

	public void moveCantoEsquerdo(Robot r) {
		r.mouseMove(746, 262);
	}

	public void percorreAnuncio(Robot r) throws InterruptedException {
		int x = pegaValorX();
		int y = pegaValorY();
		while (pegaCorPixel(r).getRed() > 200) {
			for (int i = 0; i < 92; i += 5) {
				for (int j = 0; j < 193; j += 5) {
					r.mouseMove(x + j, y + i);
					if (pegaCorPixel(r).getRed() < 200) {
						break;
					}
				}
				if (pegaCorPixel(r).getRed() < 200) {
					break;
				}
			}
		}
	}

	public void abreAnuncio(Robot r) {
		r.mousePress(InputEvent.BUTTON1_MASK);
		r.delay(100);
		r.mouseRelease(InputEvent.BUTTON1_MASK);
	}

	public Color pegaCorPixel(Robot r) {
		Color cor = r.getPixelColor(pegaValorX(), pegaValorY());
		return cor;
	}

	private int pegaValorX() {
		int x = (int) MouseInfo.getPointerInfo().getLocation().getX();
		return x;
	}

	private int pegaValorY() {
		int y = (int) MouseInfo.getPointerInfo().getLocation().getY();
		return y;
	}
}
